var express = require('express');
var router = express.Router();
var Item = require('../models/Item');
var Cart = require('../models/Cart');
var under = require("underscore");
var promotion_barcodes = ['ITEM00001','ITEM00006'];

/* GET home page. */
router.get('/', function(req, res, next) {
  res.sendfile("public/html/index.html")
});
router.get('/index', function(req, res, next) {
  res.sendfile("public/html/index.html")
});
router.get('/shopping_list', function(req, res, next) {
  res.sendfile("public/html/shopping_list.html")
});
router.get('/cart_list', function(req, res, next) {
  res.sendfile("public/html/cart_list.html")
});
router.get('/pay', function(req, res, next) {
  res.sendfile("public/html/pay.html")
});
router.get('/allItems',function(req, res, next) {
  Item.find({}, function (err, doc) {
    res.json(doc);
  });
});
router.get('/allCartList',function(req, res, next) {
  Cart.find({}, function (err, doc) {
    res.json(doc);
  });
});
router.post('/addItemToCart',function(req, res, next) {
  Cart.find({barcode:req.body.barcode}, function (err, doc) {
    if (doc.length == 0)
    {
      Item.findOne({barcode:req.body.barcode},function(err,item){
        var subtotal = item.price.toFixed(2) + "元";
        var cart_item ={barcode:item.barcode,
          name:item.name,
          unit:item.unit,
          price:item.price,
          type:item.type,
          count: 1,
          subtotal: subtotal};
        new Cart(cart_item).save(function(err,doc){
          Cart.find({},function(err,allCartList)
          {
            res.json(allCartList);
          })
        });

      });
    }else
    {
      var findOne = doc[0];
      var count = findOne.count + 1;

      var subtotal = (findOne.price * findOne.count).toFixed(2) + "元";
      var barcode = under.find(promotion_barcodes,function(barcode){
        return barcode == req.body.barcode;
      });
      if(barcode != undefined && count >= 3)
      {
        subtotal = findOne.price * (findOne.count - Math.floor(findOne.count/3))+"元（原价："
          +findOne.price * findOne.count + "元）";
      }
      var update_data = {$set:{count:count, subtotal:subtotal}};
      Cart.update({barcode: req.body.barcode},update_data,function(err,doc){
        console.log(err+"err");
        console.log(doc);
        Cart.find({},function(err,allCartList)
        {
          res.json(allCartList);
        })
      });
    }
  });
});

router.post('/addItemToCartInplus',function(req, res, next) {
  Cart.find({barcode:req.body.barcode}, function (err, doc) {
    var findOne = doc[0];
    var count = findOne.count + 1;

    var subtotal = (findOne.price * findOne.count).toFixed(2) + "元";
    var barcode = under.find(promotion_barcodes,function(barcode){
      return barcode == req.body.barcode;
    });
    if(barcode != undefined && count >= 3)
    {
      subtotal = findOne.price * (findOne.count - Math.floor(findOne.count/3))+"元（原价："
        +findOne.price * findOne.count + "元）";
    }
    var update_data = {$set:{count:count, subtotal:subtotal}};
    Cart.update({barcode: req.body.barcode},update_data,function(err,doc){
      console.log(err+"err");
      console.log(doc);
      Cart.find({},function(err,allCartList)
      {
        res.json(allCartList);
      })
    });
  });
});

router.post('/subItemToCart',function(req, res, next) {
  Cart.find({barcode:req.body.barcode}, function (err, doc) {
    var findOne = doc[0];
    var count = findOne.count - 1;
    if(count == 0)
    {
      Cart.remove({barcode:req.body.barcode},function(err, doc){

      })
    }else
    {
      var subtotal = (findOne.price * findOne.count).toFixed(2) + "元";
      var barcode = under.find(promotion_barcodes,function(barcode){
        return barcode == req.body.barcode;
      });
      if(barcode != undefined && count >= 3)
      {
        subtotal = findOne.price * (findOne.count - Math.floor(findOne.count/3))+"元（原价："
          +findOne.price * findOne.count + "元）";
      }
      var update_data = {$set:{count:count, subtotal:subtotal}};
    }
    Cart.update({barcode: req.body.barcode},update_data,function(err,doc){
      console.log(err+"err");
      console.log(doc);
      Cart.find({},function(err,allCartList)
      {
        res.json(allCartList);
      })
    });
  });
});

router.post("/emptyCart",function(req, res, next) {
  Cart.remove({},function(err,doc){
    Cart.find({},function(err,allCartList)
    {
      res.json(allCartList);
    })
  })
});


//var allItems = [{type:"饮料",name:"可口可乐",price:3,unit:"瓶",barcode:"ITEM00001"},
//  {type:"饮料",name:"雪碧",price:3,unit:"瓶",barcode:"ITEM00002"},
//  {type:"水果",name:"苹果",price:5.5,unit:"斤",barcode:"ITEM00003"},
//  {type:"水果",name:"荔枝",price:15,unit:"斤",barcode:"ITEM00004"},
//  {type:"生活用品",name:"电池",price:2,unit:"个",barcode:"ITEM00005"},
//  {type:"食品",name:"方便面",price:4.5,unit:"袋",barcode:"ITEM00006"}];
//for(var i = 0; i < allItems.length; i ++)
//{
//  new Item(allItems[i]).save(function(err, c){
//    console.log(err)
//    console.log(c)
//  })
//}


module.exports = router;
